package com.intranet.sys.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.sys.domain.User;
import com.intranet.sys.service.UserService;
import com.water.dao.Page;
import com.water.domain.DataTableParameter;
import com.water.web.Tool;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 查询所有用户
	 * 
	 * @param response
	 * @param page
	 * @param rows
	 */
	@RequestMapping("/getAllUser")
	public void getAllUser(HttpServletResponse response,
			DataTableParameter param) {

		// 搜索条件
		String search = param.getSSearch();
		// 查询结果集
		Page page = userService.getAllUser(param.getPageNo(), param
				.getIDisplayLength(), search);
		List<User> users = page.getData();
		List<Object> list = new ArrayList<Object>();
		for (User u : users) {
			list.add(new String[] {
					"<input name=\"checkbox\" type=\"checkbox\" class=\"checkboxes\" value=\""
							+ u.getId().toString() + "\" />", u.getRealname(),
					u.getUsername(), u.getEmail() });
		}
		// 封装页面参数
		String aaData = JSONArray.fromObject(list).toString();
		JSONObject json = new JSONObject();
		json.put(DataTableParameter.SECHO, param.getSEcho());
		json.put(DataTableParameter.ITOTALRECORDS, page.getTotalCount());// 过滤前总记录数
		json.put(DataTableParameter.ITOTALDISPALYRECORDS, page.getTotalCount());// 过滤后总记录数
		json.put(DataTableParameter.AADATA, aaData);
		Tool.writeByAction(json.toString(), response);
	}

	/**
	 * 加载用户
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/loadUser")
	public ModelAndView loadUser(HttpServletRequest request,
			@RequestParam(value = "id", required = true)
			int id, @RequestParam(value = "type", required = true)
			String type) {
		ModelAndView mav = new ModelAndView();
		User user = userService.getUserById(id);
		mav.addObject("user", user);
		if (type.equals("edit")) {
			mav.setViewName("/sys/user/editUser");
		} else {
			mav.setViewName("/sys/user/showUser");
		}
		return mav;
	}

	/**
	 * 新建页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/addUser")
	public ModelAndView addUser(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/sys/user/addUser");
		return mav;
	}

	/**
	 * 保存用户
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/saveUser")
	public void saveUser(HttpServletResponse response, User user) {
		String msg = "N";
		if (user.getId() == null) {
			User u = new User();
			u.setRealname(user.getRealname());
			u.setUsername(user.getUsername());
			u.setPassword(user.getPassword());
			u.setEmail(user.getEmail());
			u.setEmailPwd(user.getEmailPwd());
			userService.saveUser(u);
			msg = "Y";
		} else {
			User u = userService.getUserById(user.getId());
			u.setRoleId(user.getRoleId());
			u.setGender(user.getGender());
			u.setRealname(user.getRealname());
			u.setPassword(user.getPassword());
			u.setPhone(user.getPhone());
			u.setState(user.getState());
			userService.updateUser(u);
			msg = "Y";
		}
		JSONObject o = new JSONObject();
		o.put("Message", msg);
		o.put("ReturnUrl", msg);
		Tool.writeByAction(o.toString(), response);
	}

	/**
	 * 删除用户
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteUser")
	public void deleteUser(HttpServletResponse response,
			@RequestParam(value = "ids", required = false)
			String ids) {
		String msg = "";
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			User u = userService.getUserById(Integer.valueOf(id[i]));
			msg = userService.deleteUser(u);
		}
		Tool.writeByAction(msg, response);
	}

}
